package com.example.bbook.services.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.bbook.entity.AddrEntity;
import com.example.bbook.entity.GoodsEntity;
import com.example.bbook.services.CRUDServiceInterface;
import com.example.bbook.util.PageTool;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.HashMap;

@Service
public class AddrServiceImpl implements CRUDServiceInterface<AddrEntity> {
    @Autowired
    private BaseMapper<AddrEntity> baseMapper;

    @Override
    public HashMap<String, Object> findAll(PageTool pageConfig, AddrEntity data) {
        Integer page = 1, pageSize = 10;
        if (pageConfig.normal()) {
            page = pageConfig.getPage();
            pageSize = pageConfig.getPageSize();
        }
        IPage<AddrEntity> userPage = new Page<>(page, pageSize);
        QueryWrapper<AddrEntity> userQueryWrapper = new QueryWrapper<>();
        if (data.getUserId() != null && data.getUserId() > 0) {
            userQueryWrapper.eq("user_id", data.getUserId());
        }
        userPage = baseMapper.selectPage(userPage, userQueryWrapper);
        HashMap<String, Object> resMap = new HashMap<>();
        resMap.put("recode", userPage.getRecords());
        resMap.put("columns", new AddrEntity().thisColumns());
        resMap.put("total", baseMapper.selectCount(userQueryWrapper));
        return resMap;
    }

    @Override
    public Boolean add(AddrEntity data) {
        return baseMapper.insert(data) > 0;
    }

    @Override
    public Boolean deleteByIds(Integer[] ids) {
        return baseMapper.deleteBatchIds(Arrays.asList(ids)) == ids.length;
    }

    @Override
    public Boolean updateById(AddrEntity data) {
        return baseMapper.updateById(data) == 1;
    }
}
